/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.dialog.io.controller;

import offset.nodes.client.dialog.io.view.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ResourceBundle;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import offset.nodes.client.model.ServerModel;
import offset.nodes.client.model.ServerResponse;
import offset.nodes.client.dialog.io.model.ImportData;
import offset.nodes.client.dialog.io.model.ImportNode;
import offset.nodes.client.view.BaseApplet;

/**
 *
 * @author Walter Lütgenau
 */
public class ImportApplet extends BaseApplet {

    JFileChooser dialog = null;
    ServerModel model = null;
    String fromPath = null;
    ResourceBundle bundle = ResourceBundle.getBundle("offset/nodes/client/dialog/view/DialogBundle");

    /** Creates a new instance of NodeChooserApplet */
    public ImportApplet() {
    }

    @Override
    public void startUI() {
        dialog.setDialogType(JFileChooser.OPEN_DIALOG);
        int result = dialog.showDialog(new JFrame(), bundle.getString("import.title"));
        URL path = null;
        try {
            if (result == JFileChooser.APPROVE_OPTION) {
                File selected = dialog.getSelectedFile();
                ImportPanel importPanel = (ImportPanel) dialog.getAccessory();

                ImportNode.Request importNodeRequest = new ImportNode.Request();
                importNodeRequest.setUuidBehaviour(importPanel.getUuidBehaviour());
                importNodeRequest.setPath(fromPath);

                ImportNode.Response importNodeResponse = (ImportNode.Response) model.sendRequest(importNodeRequest);
                if (importNodeResponse.getResult() == ServerResponse.RESULT_SUCCESS) {
                    InputStream in = new FileInputStream(selected);
                    ImportData.Request importDataRequest = new ImportData.Request();

                    byte[] buf = null;

                    int size = (int) selected.length();
                    while (size > 0) {
                        importDataRequest.setDataId(importNodeResponse.getImportId());

                        int length = Math.min(ImportData.PACKAGE_SIZE, size);
                        if (buf == null || buf.length != length)
                            buf = new byte[length];

                        in.read(buf);

                        importDataRequest.setData(buf);
                        if (length == size)
                            importDataRequest.setLastFragment(true);

                        ImportData.Response importDataResponse = (ImportData.Response) model.sendRequest(importDataRequest);
                        if (importDataResponse.getResult() != ServerResponse.RESULT_SUCCESS)
                            break;

                        size -= length;
                    }

                    in.close();
                }

            }

            path = new URL(getParameter("repository") + "/" + fromPath);

        } catch (Exception e) {
            e.printStackTrace();
        }

        getAppletContext().showDocument(path);
    }

    @Override
    public void initUI() {
        try {
            fromPath = getParameter("path");
            String url = getParameter("service");

            model = new ServerModel(new URL(url));
            dialog = new JFileChooser();
            dialog.setAccessory(new ImportPanel());
            dialog.setLocation(100, 100);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
